package day0428.com.ch16threeLayer.service.Impl;

import day0428.com.ch16threeLayer.dao.IUserDao;
import day0428.com.ch16threeLayer.dao.Impl.UserDao;
import day0428.com.ch16threeLayer.entity.User;
import day0428.com.ch16threeLayer.service.IUserService;

import java.util.List;

/**
 * 作者：wangmaokang
 * 日期: 2021/4/28 10:06
 * 描述:
 */
public class UserService implements IUserService {

    @Override
    public User login(String name, String passWord) {
        IUserDao userDao=new UserDao();
        return userDao.login(name,passWord);
    }

    @Override
    public User getUserById(int id) throws Exception {
        IUserDao userDao=new UserDao();
        User user=userDao.getUserById(id);
        if (user==null){
            throw new Exception("没有找到用户");
        }

        return user;
    }

    @Override
    public List<User> getAllUsers() throws Exception {
        IUserDao userDao=new UserDao();
        List<User> list=userDao.getAllUsers();
        if (list==null||list.size()==0){
            throw new Exception("没有用户");
        }
        return list ;
    }

    @Override
    public void addUser(User user) throws Exception {
        IUserDao userDao=new UserDao();

        User user2=userDao.getUserById(user.getId());

        if (user2!=null){
            throw new Exception("该用户id 已经存在");
        }

        userDao.addUser(user);

    }

    @Override
    public void deleteUserById(int id) throws Exception {
        IUserDao userDao=new UserDao();
        User user2=userDao.getUserById(id);
        if (user2!=null){
            throw new Exception("该用户id 已经存在");
        }
        userDao.deleteUserById(id);
    }
}
